package com.breaktian.demoapp.ui;

import android.graphics.Color;
import android.support.v4.app.FragmentTabHost;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.breaktian.assemble.adapter.PermissionAdapter;
import com.breaktian.assemble.jni.Native;
import com.breaktian.assemble.utils.LogUtil;
import com.breaktian.demoapp.R;
import com.breaktian.demoapp.provider.HomeProvider;
import com.breaktian.demoapp.provider.ProviderFactory;
import com.breaktian.demoapp.ui.base.BaseActivity;
import com.breaktian.demoapp.ui.fragment.ContactsFragment;
import com.breaktian.demoapp.ui.fragment.FindFragment;
import com.breaktian.demoapp.ui.fragment.IndexFragment;
import com.breaktian.demoapp.ui.fragment.MineFragment;

/**
 * 数据提供者 HomeProvider
 * */
public class HomeActivity extends BaseActivity<HomeProvider> implements TabHost.OnTabChangeListener {

    private Class mFragment[] = {IndexFragment.class, ContactsFragment.class, FindFragment.class, MineFragment.class};
    private int mImage[] = {R.drawable.iv_index_selector, R.drawable.iv_contacts_selector, R.drawable.iv_find_selector, R.drawable.iv_mine_selector};
    private String mTitle[] = {"首页", "通讯录", "发现", "我的"};

    private int mCurIndex = 0;

    private HomeActivity context;
    private FragmentTabHost tabhost;
    private FrameLayout realtabcontent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        this.context = this;
        initView();
//        ToastUtil.show("jni:"+ NativeUtils.sayHello());

        byte[] key = Native.getThreeDesKey();
//        ToastUtil.show("jni three des key:"+ new String(key));

        requestPermission();

    }

    private void requestPermission() {
        mPermissionAdapter.requestStoragePermission(new PermissionAdapter.PermissionVerifyCallBack() {
            @Override
            public void onPermissionGet() {
                LogUtil.i("get");
            }

            @Override
            public void onPermissionLost() {
                LogUtil.i("lost");
            }
        });
    }

    private void initView() {
        tabhost = (FragmentTabHost)findViewById(R.id.tabhost);
        realtabcontent = (FrameLayout)findViewById(R.id.realtabcontent);
        tabhost.setup(context, getSupportFragmentManager(), R.id.realtabcontent);
        tabhost.getTabWidget().setDividerDrawable(null);
        for(int i=0;i<mFragment.length;i++){
            TabHost.TabSpec tabSpec = tabhost.newTabSpec(mTitle[i]).setIndicator(getTabView(i));
            tabhost.addTab(tabSpec, mFragment[i], null);
        }

        tabhost.setBackgroundColor(Color.parseColor("#00FF0000"));
        //设置监听
        tabhost.setOnTabChangedListener(this);
        tabhost.setCurrentTab(mCurIndex);
        updateTabTextColor(tabhost);
    }

    @Override
    public void onTabChanged(String tabId) {
        LogUtil.d("tabId:"+tabId);
        updateTabTextColor(tabhost);
        if(TextUtils.isEmpty(tabId)) return;
        for(int i=0;i<mTitle.length;i++){
            if(tabId.equals(mTitle[i]))
                mCurIndex = i;
        }
    }




    @Override
    protected HomeProvider getProvider() {
        return ProviderFactory.newHomeProvider();
    }

    /**
     * 获得每一个tab的View
     * @param index
     */
    private View getTabView(int index){
        View view = LayoutInflater.from(this).inflate(R.layout.tab_view, null);
        ImageView iv_icon = (ImageView)view.findViewById(R.id.iv_icon);
        iv_icon.setImageResource(mImage[index]);
        TextView tv_title = (TextView)view.findViewById(R.id.tv_title);
        tv_title.setText(mTitle[index]);
        return view;
    }

    /**
     * 更新Tab标签的颜色，和字体的颜色
     * @param tabHost
     */
    private void updateTabTextColor(final TabHost tabHost) {
        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(R.id.tv_title);
            tv.setTextSize(14);
            if (tabHost.getCurrentTab() == i) {//选中
                tv.setTextColor(this.getResources().getColor(R.color.tab_text_color_selected));
            } else {//不选中
                tv.setTextColor(this.getResources().getColor(R.color.tab_text_color));
            }
        }
    }


}
